﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;

namespace Kooboo.Commerce.AddIn
{
    public static class PaymentMethodExtensions
    {
        public static MvcHtmlString RenderPayment(this ViewContext viewContext, IPaymentMethod method)
        {
            var m = method as IDirectPaymentMethod;
            if (m == null)
            {
                return MvcHtmlString.Empty;
            }
            else
            {
                ControllerContext controllerContext = new ControllerContext(viewContext.RequestContext,m.FormController);
                var view = new WebFormView(controllerContext,"/Payments/" + method.Name + "/" + m.FormViewName);
                var html = new StringBuilder();
                using (TextWriter writer = new StringWriter(html))
                {
                    var methodViewContext = new ViewContext(controllerContext, view, new ViewDataDictionary(m.FormModel), viewContext.TempData, writer);

                    view.Render(methodViewContext, writer);

                    return MvcHtmlString.Create(html.ToString());
                }
            }
        }
    }
}
